<?php

class Welcome extends PC_Controller {





	public function index()
	{
		$data['site_config'] = $this->config->item('site_setting');
		$this->load->view('login',$data);
	}


    public function userLogin(){

		$data = $this->input->post();
		if(!$data['username'] || !$data['password'] || !($data['module']=='service')){
			jumpurlerror('参数不完整',2,'');
			return;
		}


        $this->load->model('site_service_model');
		$userinfo = $this->site_service_model->userLogin($data['username']);
		//var_dump($userinfo);
        if(!$userinfo){
			jumpurlerror('登录帐号错误',2,'');
			return;
		}else{
			$password = md5($data['password']);
			if($userinfo->password!=$password){
				jumpurlerror('密码不正确',2,'');
				return;
			}
			//禁止多端登陆
			$check_token = $this->db->where(['mobile'=>$userinfo->mobile])->limit(1)->get("site_service_relation");
            if($check_token->row()){
				$check_token = $check_token->row();
				$current_token = $check_token->token;
				$connection_info =  $this->db->where(['token'=>$current_token])->limit(1)->get("user_connection");
				if($connection_info->row()){
					jumpurlerror('您已在其它地方登录,当前系统禁止多点登录！',2,'');
					return;
				}
			}else{
				jumpurlerror('系统异常',2,'');
				return;
			}
			$userinfo->token = md5(uniqid().time());
			//写入token到聊天权限
            $this->load->model('site_service_relation_model');
			$this->site_service_relation_model->update(['site_id'=>$userinfo->site_id,'token'=>$userinfo->token]);
			$_SESSION['service']= $userinfo;
			$home_url = base_url('admin/index');
			redirect($home_url);
		}

	}





}
